/********************************************************/
/**    SNMP Manager                                    **/
/**                                                    **/
/** Included libraries in the project:                 **/
/**     libcomctl32.a   libwinm.a                      **/
/**     libsnmpapi.a    libmgmtapi.a    libadvapi.a    **/
/**                                                    **/
/********************************************************/


#include "../inc/main.h"


/*  Make the class name into a global variable  */
char szClassName[ ] = "SNMP Manager";
extern unsigned int nbItems;
char console_string[SIZE_STRING];
/* Main function */
int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName =NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "SNMP Manager",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           WINDOW_WIDTH,                 /* The programs width */
           WINDOW_HEIGHT,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    //ShowWindow (hwnd, nCmdShow);
    initComctrls();
    /* Load dialogbox according to the resource file */
    /* This dialog box is the main window of the program */
    DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(DIALOG_ID), hwnd, mainWindowCallback);
    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}



void saveLog(HWND hwnd)
{

    char element[SIZE_STRING];
    char fileName[SIZE_STRING];
    unsigned int i,j;
    struct tm *local;
    time_t t;
    FILE* save_file;
    HWND listView = GetDlgItem(hwnd,ID_LISTVIEW);  //pointer to the listView
    LVITEM lvi;
    lvi.mask = LVIF_TEXT;


    t = time(NULL);
    local = localtime(&t);
    strcpy(fileName,"snmp_log ");
    strcat(fileName,(char *)asctime(local));
    //Remove spaces and unwanted characters
    fileName[strlen("snmp_log ")+13]='h';
    fileName[strlen("snmp_log ")+16]='m';
    fileName[strlen("snmp_log ")+17]=0;
    strcat(fileName,".txt");
    sprintf(console_string,"Saving %s...",fileName);
    cPrintf (hwnd, console_string);
    save_file = fopen (fileName, "w+");

    if(save_file)
    {
        fprintf(save_file,"--------------- SNMP Manager Walk --------------- \n\nRecorded on %s\n\tName\tType\tVariable\n",asctime(local));
        for(i=0;i<nbItems;i++)
        {
            for(j=0;j<3;j++)
            {
                ListView_GetItemText(listView,i,j,element,SIZE_STRING);
                fprintf(save_file,"\t%s", element);
            }
            fprintf(save_file,"\n");
        }
        sprintf(console_string,"File saved!");
        cPrintf (hwnd, console_string);
        fclose(save_file);
    }else
    {
        sprintf(console_string,"Unable to read/open the file!");
        cPrintf (hwnd, console_string);
    }
}









